package cn.cmkj.auction.adapter;

import android.content.Context;
import android.content.Intent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

import com.jude.rollviewpager.adapter.StaticPagerAdapter;

import org.xutils.image.ImageOptions;
import org.xutils.x;

import java.util.List;

import cn.cmkj.auction.R;
import cn.cmkj.auction.data.http.MainAdvertise;
import cn.cmkj.auction.ui.BrowserActivity;

/**
 * Created by cunguoyao on 2017/8/30.
 */

//适配器
public class MainRollPagerAdapter extends StaticPagerAdapter {

    private List<MainAdvertise> imageUrls;
    private Context context;
    private ImageOptions imageOptions;

    public MainRollPagerAdapter(Context context, List<MainAdvertise> imageUrls) {
        this.imageUrls = imageUrls;
        this.context = context;
        this.imageOptions = new ImageOptions.Builder()
                .setFadeIn(true).setFailureDrawableId(R.drawable.empty_photo)
                .setImageScaleType(ImageView.ScaleType.FIT_XY)
                .build(); //淡入效果
    }

    @Override
    public View getView(ViewGroup container, int position) {
        ImageView imageView = new ImageView(container.getContext());
        if(imageUrls != null && imageUrls.size() > 0) {
            final MainAdvertise rollAd = imageUrls.get(position);
            x.image().bind(imageView, rollAd.url, imageOptions);
            imageView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    Intent intent = new Intent(context, BrowserActivity.class);
                    intent.putExtra(BrowserActivity.URL, rollAd.link_sub_url);
                    context.startActivity(intent);
                }
            });
        }
        return imageView;
    }

    @Override
    public int getCount() {
        return imageUrls == null ? 0 : imageUrls.size();
    }
}
